<?php
/************************************
*   熊店
*   @file:/server/bearshop/__include/data_model/mod_user.php
*   @intro:
*   @author:new_shop
*   @email:new_shop@163.com    
*   @date:Fri Feb 12 10:27:58 CST 2010
************************************/

class mod_user extends data_model 
{
    private $aCommonField = array('id' , 'email' , 'stat' ,'nick', 'is_designer' , 'ctime' , 'login_time');
    

    
    public function __construct($db_config)
    {
        parent::__construct($db_config);
        $this->_table = 'bsc_user_basic';
    }

    /**
     * 创建用户记录
     *
     * @param int $uid
     * @param array $info   //目前不用
     * @return bool
     */
    public function create_user($email , $password , $nick , &$uid , $info=array())
    {
        $this->start(K5_DATAMOD_NOHASH);
        $this->set('nick' , $nick);
        $this->set('email' , $email);
        $this->set('passwd' , md5($password));
        $this->set('ctime' , time());
        $this->set('cip' , K::get_ip());
        $rs = $this->insert();
        if(!$rs)
            return false;
            
        $uid = $this->get_insert_id();
        return true;
    }
    /**
     * 检查EMAIL
     *
     * @param string $email
     * @param bool $is_exists
     * @return bool
     */
    public function check_email($email , &$is_exists)
    {
        $this->start(K5_DATAMOD_NOHASH);
        $this->set_condition('email="'.$email.'"');
        $rs = $this->fetch_count($n);
        if(!$rs)
            return false;
        
        $is_exists = $n > 0 ? true : false;
        return true;
    }
    /**
     * 登录验证
     *
     * @param string $email
     * @param string $passwd
     * @param array $info
     * @return unknown
     */
    public function login($email , $passwd , &$info)
    {
        $info = array();
        $this->start(K5_DATAMOD_NOHASH);
        $this->set_condition('email="'.$email.'" AND passwd="'.md5($passwd).'"');
        $rs = $this->fetch($this->aCommonField);
        if(!$rs)
            return false;
            
        $info = $this->get_data();
        $info['uid'] = $info['id'];
        unset($info['id']);
        return true;
    }
    /**
     * 更新最后登录时间
     *
     * @param int $uid
     * @return bool
     */
    public function update_last_login($uid)
    {
        $this->start(K5_DATAMOD_NOHASH);
        $this->set_condition('id='.$uid);
        $this->set('login_time' , time());
        $this->set('login_ip' , K::get_ip());
        return $this->update();
    }
    /**
     * 取单个用户信息
     *
     * @param unknown_type $uid
     * @param unknown_type $info
     * @return unknown
     */
    public function get_user($uid , &$info)
    {
        $this->start(K5_DATAMOD_NOHASH);
        $this->set_condition('id='.$uid);
        $rs = $this->fetch($this->aCommonField);
        if(!$rs)
            return false;
        $info = $this->get_data();
        return true;
    }
    /**
     * 编辑单值
     *
     * @param int $uid
     * @param string $field
     * @param mix $value
     * @return bool
     */
    public function edit_value($uid , $field , $value)
    {
        $this->start(K5_DATAMOD_NOHASH);
        $this->set_condition('id='.$uid);
        $this->set($field , (int)$value);
        $this->set('utime' , time());
        $this->set('uip' , K::get_ip());
        return $this->update();
    }
    
    public function get_nick($uid , &$nick)
    {
        $this->start();
        $this->set_condition('id='.$uid);
        $rs = $this->fetch(array('nick'));
        $nick = $rs ? $this->get('nick') : $uid;
        return true;
    }
}
?>